﻿using LatteCoffee.coffeemodel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LatteCoffee
{
    public class SocketClients
    {
        private static SocketClients instance;
        private SocketClients() { }
        public static SocketClients Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SocketClients();
                }
                return instance;
            }
        }
        /// <summary>
        /// 创建客户端
        /// </summary>
        public Socket client;
        public void socketclient()
        {
            try
            {
                ///创建客户端
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                ///IP地址
                IPAddress ip = IPAddress.Parse("192.168.3.7");
                ///端口号
                IPEndPoint endPoint = new IPEndPoint(ip, 8885);
                ///建立与服务器的远程连接
                client.Connect(endPoint);
                ClientList clientList = new ClientList();
                clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                clientList.code = 4;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                ///线程问题
                Thread thread = new Thread(ReciveMsg);
                thread.IsBackground = true;
                thread.Start(client);
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 客户端接收到服务器发送的消息
        /// </summary>
        /// <param name="o">客户端</param>
        void ReciveMsg(object o)
        {
            Socket client = o as Socket;
            while (true)
            {
                try
                {
                    ///定义客户端接收到的信息大小
                    byte[] arrList = new byte[1024 * 1024];
                    ///接收到的信息大小(所占字节数)
                    int length = client.Receive(arrList);
                    string msg = Encoding.UTF8.GetString(arrList, 0, length);
                    var ClientList = JsonConvert.DeserializeObject<ClientList>(msg);
                    if (ClientList != null)
                    {
                        if (ClientList.code == 0)
                        {
                            show(ClientList.message.type, ClientList.message.Name);
                        }
                        else if (ClientList.code == 3)
                        {
                            RobotShow(ClientList.message.Name, ClientList.message.type);
                            ClientList clientList = new ClientList();
                            clientList.message.Name = ClientList.message.Name;
                            clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                            clientList.robotMsg = RobotMsg.Instance;
                            clientList.code = 3;
                            clientList.IsMake = Form1.isMaking;
                            var list = JsonConvert.SerializeObject(clientList);
                            var msgs = Encoding.UTF8.GetBytes(list);
                            client.Send(msgs);
                        }
                    }
                }
                catch (Exception)
                {
                    ///关闭客户端
                    client.Close();
                }

            }
        }
        public void show(int code, string msg)
        {
            try
            {
                Form1.AllBol = false;
                Thread.Sleep(100);
                switch (msg)
                {
                    case "JB_BTN":
                        if (code == 0)
                        {
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
                            Thread.Sleep(100);
                            Form1.AllBol = true;
                        }
                        else
                        {
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            Form1.AllBol = true;
                        }
                        break;
                    case "XB_BTN":
                        if (code == 0)
                        {
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CupWashingPump_start);
                            Thread.Sleep(100);
                            Form1.AllBol = true;
                        }
                        else
                        {
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.CupWashingPump_end);
                            Thread.Sleep(100);
                            Form1.AllBol = true;
                        }
                        break;
                    case "SB_BTN":
                        if (code == 0)
                        {
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_start);
                            Thread.Sleep(100);
                            Form1.AllBol = true;
                        }
                        else
                        {
                            Form1.AllBol = false;
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                            Thread.Sleep(100);
                            ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                            Thread.Sleep(100);
                            Form1.AllBol = true;
                        }
                        break;
                    case "ZDSB_BTN":
                        Console.WriteLine("OK1");
                        Console.WriteLine("1");
                        Console.WriteLine("112");
                        Console.WriteLine("12");
                        Form1.AllBol = false;
                        Thread.Sleep(100);
                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                        Thread.Sleep(100);
                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                        Thread.Sleep(100);
                        Form1.AllBol = true;
                        Console.WriteLine(WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol);
                        if (!WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                        {
                            Console.WriteLine("16");
                            if (WuLiaoCoffeeSel.Instance.MotorUP1_Sel_Bol)
                            {
                                Console.WriteLine("17");
                                Form1.AllBol = false;
                                Thread.Sleep(100);
                                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
                                Console.WriteLine("yes3");
                                Thread.Sleep(100);
                                Form1.AllBol = true;

                                Thread.Sleep(100);
                                Console.WriteLine("19");
                                while (true)
                                {
                                    Console.WriteLine("20");
                                    if (WuLiaoCoffeeSel.Instance.MotorOriginalPoint1_Sel_Bol)
                                    {
                                        Console.WriteLine("20");
                                        //WuLiaoCoffeeSel.Instance.UPCupBtn1_Sel_Bol = false;

                                        break;
                                    }
                                    else
                                    {
                                        Thread.Sleep(100);
                                    }
                                    Thread.Sleep(10);
                                }
                                Console.WriteLine("20");
                                Form1.AllBol = false;
                                Thread.Sleep(100);
                                ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                                Console.WriteLine("yes3");
                                Thread.Sleep(100);
                                Form1.AllBol = true;
                                cupbol1 = false;
                            }
                            else
                            {
                                if (!cupbol1)
                                {
                                    Form1.AllBol = false;
                                    Thread.Sleep(100);
                                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_start);
                                    Console.WriteLine("yes3");
                                    Thread.Sleep(100);
                                    Form1.AllBol = true;
                                    while (true)
                                    {
                                        Console.WriteLine("22");
                                        if (WuLiaoCoffeeSel.Instance.MotorUP1_Sel_Bol)
                                        {
                                            cupbol1 = true;
                                            break;
                                        }
                                        else if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                                        {
                                            Thread.Sleep(100);
                                            break;
                                        }
                                        else
                                        {
                                            Thread.Sleep(100);
                                        }
                                        Thread.Sleep(10);
                                    }
                                    Form1.AllBol = false;
                                    Thread.Sleep(100);
                                    ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Impulse1_end);
                                    Console.WriteLine("yes3");
                                    Thread.Sleep(100);

                                }
                                if (cupbol1 && !WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                                {
                                    if (!WuLiaoCoffeeSel.Instance.MotorOriginalPoint1_Sel_Bol)
                                    {
                                        Console.WriteLine("17");
                                        Form1.AllBol = false;
                                        Thread.Sleep(100);
                                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
                                        Console.WriteLine("yes3");
                                        Thread.Sleep(100);
                                        Form1.AllBol = true;

                                        Thread.Sleep(1000);
                                        Console.WriteLine("19");
                                        while (true)
                                        {
                                            Console.WriteLine("20");
                                            if (WuLiaoCoffeeSel.Instance.MotorOriginalPoint1_Sel_Bol)
                                            {
                                                Console.WriteLine("20");
                                                // WuLiaoCoffeeSel.Instance.UPCupBtn1_Sel_Bol = false;
                                                break;
                                            }
                                            else
                                            {
                                                Thread.Sleep(100);
                                            }
                                            Thread.Sleep(10);
                                        }
                                        Console.WriteLine("20");
                                        Form1.AllBol = false;
                                        Thread.Sleep(100);
                                        ElseSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                                        Console.WriteLine("yes3");
                                        Thread.Sleep(100);
                                        Form1.AllBol = true;
                                    }
                                    cupbol1 = false;
                                }
                                Form1.AllBol = true;

                                Thread.Sleep(1000);
                            }
                        }
                        Console.WriteLine("完成0");
                       Form1.CupBol1 = true;
                        break;
                    default:
                        break;
                }
                Thread.Sleep(100);
                Form1.AllBol = true;
                ClientList clientList = new ClientList();
                MessageClientList coffeelist = MessageClientList.Instance;
                coffeelist.Name = msg;
                coffeelist.type = code == 1 ? 0 : 1;
                clientList.message = coffeelist;
                clientList.code = 0;
                client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
            }
            catch (Exception)
            {
            }
        }
        public static bool cupbol1 = false;
        public static bool CupBol1 = true;
        /// <summary>
        ///   状态  0 关状态  1 开状态 2 已开机 3 上电 4 下电 5 已使能 6 下使能 7 已运行 8 继续运行 9 已报警 10 已暂停  11已停止 ,12 机械臂状态查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="Type"></param>
        public void RobotShow(string name, int Type)
        {
            try
            {
                if (name == "Reload_L")
                {
                    switch (Type)
                    {
                        case 3:
                            jakaAPI.power_on(ref RobotOperate.handle1);
                            break;
                        case 4:
                            jakaAPI.power_off(ref RobotOperate.handle1);
                            break;
                        case 5:
                            jakaAPI.enable_robot(ref RobotOperate.handle1);
                            break;
                        case 6:
                            jakaAPI.disable_robot(ref RobotOperate.handle1);
                            break;
                        case 7:
                            jakaAPI.program_run(ref RobotOperate.handle1);
                            break;
                        case 8:
                            jakaAPI.program_resume(ref RobotOperate.handle1);
                            break;
                        case 9:
                            jakaAPI.clear_error(ref RobotOperate.handle1);
                            break;
                        case 10:
                            jakaAPI.program_pause(ref RobotOperate.handle1);
                            break;
                        case 11:
                            jakaAPI.program_abort(ref RobotOperate.handle1);
                            break;
                    }
                    RobotOperate.Robot_L_Show();
                }
                else
                {
                    switch (Type)
                    {
                        case 3:
                            jakaAPI.power_on(ref RobotOperate.handle2);
                            break;
                        case 4:
                            jakaAPI.power_off(ref RobotOperate.handle2);
                            break;
                        case 5:
                            jakaAPI.enable_robot(ref RobotOperate.handle2);
                            break;
                        case 6:
                            jakaAPI.disable_robot(ref RobotOperate.handle2);
                            break;
                        case 7:
                            // jakaAPI.program_run(ref RobotOperate.handle2);
                            break;
                        case 8:
                            jakaAPI.program_resume(ref RobotOperate.handle2);
                            break;
                        case 9:
                            jakaAPI.clear_error(ref RobotOperate.handle2);
                            break;
                        case 10:
                            jakaAPI.program_pause(ref RobotOperate.handle2);
                            break;
                        case 11:
                            jakaAPI.program_abort(ref RobotOperate.handle2);
                            break;
                    }
                    RobotOperate.Robot_R_Show();
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
